package com.zhkj.shixi.tvplayer.PicturePlayer;

import android.media.MediaPlayer;
import android.util.Log;

import com.zhkj.shixi.tvplayer.Interface.IMusicBackGround;

import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class MusicBackGround implements IMusicBackGround{
    MediaPlayer mediaPlayer = new MediaPlayer();
    List<File> fileResult = new ArrayList<File>();

    public List<File> getFileResult() {
        return fileResult;
    }

    public void fileFilterF(final String path){
        List<File> files = Arrays.asList(new File(path).listFiles(new typeFilter()));
        for (File fileIndex : files) {
            //如果这个文件是目录，则进行递归搜索
            if (fileIndex.isDirectory()) {
                fileFilterF(fileIndex.getAbsolutePath());
            }
            else{
                fileResult.add(fileIndex);
            }
        }
    }

    @Override
    public void playMusic(File file) {
        mediaPlayer.reset();
        try {
            mediaPlayer.setDataSource(file.getAbsolutePath());
            mediaPlayer.prepare();
        }catch (IOException e){
            e.printStackTrace();
        }
        mediaPlayer.start();
    }

    @Override
    public void stopMusic(){
        mediaPlayer.stop();
    }

    private static class typeFilter implements FileFilter
    {
        private String fileType;

        private List<String> anyType = new ArrayList<>();
        public typeFilter() {
            final List<String> musicType =  new ArrayList<>();
            musicType.add(".mp3");
            musicType.add(".wav");
            musicType.add(".ogg");
            musicType.add(".ape");
            musicType.add(".flac");
            anyType = musicType;
        }
        public boolean accept(File file) {
            for(int i = 0;i < anyType.size();i++) {
                if(file.getName().toLowerCase().endsWith(anyType.get(i))){
                    return true;
                }
                else if(file.isDirectory()){
                    return true;
                }
            }
            return false;
        }
    }


}
